<?php

  /**
   * Description of UtenteManager
   *
   * @author rocco
   */
  require_once '../classes/UtenteDataManager.php';

  class UtenteManager
  {

    private static $instance;

    public static function getInstance()
    {
      if (self::$instance == null)
      {
        $c = __CLASS__;
        self::$instance = new $c;
      }
      return self::$instance;
    }

    public function checkLogin($connection, $username, $password, $scuola)
    {
      $result = false;
      try
      {
        $passwordMD5 = md5($password);
        $result = UtenteDataManager::getInstance()->checkLogin($connection, $username, $passwordMD5, $scuola);
      }
      catch (Exception $ex)
      {
        throw $ex;
      }
      return $result;
    }

    public function insertUtente($connection, $utente)
    {
      UtenteDataManager::getInstance()->insertUtente($connection, $utente);
      $header = "From: IperTestOnLine<info@softwareeasy.it>\n";
      $header .= "MIME-Version: 1.0\n";
      $header .= "Content-Type: text/html;  charset=UTF-8\n";
      $header .= "Content-Transfer-Encoding: 7bit\n\n";
      $oggetto = "nuova registrazione Iper Test On Line";
      $messaggio = '<html><body>Gentile ' . $utente->getNome() . ' ' . $utente->getCognome() . '<br /><br />è stato creato un nuovo account sulla piattaforma Iper Test on Line. <br /><br />Username: ' . $utente->getUsername() . '<br />Password : ' . $utente->getPassword() . '<br /><br />Per effettuare il login clicca <a href="www.softwareeasy.it/school/portal/newLogin.php">qui</a><br /><br />Distinti saluti<br/>Iper Test On Line Admin</body></html>';

      mail($utente->getEmail(), $oggetto, $messaggio, $header);
    }

    public function getUtentiByData($connection, $from, $to)
    {
      return UtenteDataManager::getInstance()->getUtentiByData($connection, $from, $to);
    }

    public function associaUtenteClasse($connection, $codiceUtente, $codiceClasse)
    {
      UtenteDataManager::getInstance()->associaUtenteClasse($connection, $codiceUtente, $codiceClasse);
    }

    public function getUtenteClasse($connection, $codiceUtente)
    {
      return UtenteDataManager::getInstance()->getUtenteClasse($connection, $codiceUtente);
    }

    public function eliminaUtenteClasse($connection, $codiceUtente, $codiceClasse)
    {
      UtenteDataManager::getInstance()->eliminaUtenteClasse($connection, $codiceUtente, $codiceClasse);
    }

    public function insertStudente($db, $nome, $cognome, $email, $codiceClasse, $username, $password)
    {
      UtenteDataManager::getInstance()->insertStudente($db, $nome, $cognome, $email, $codiceClasse, $username, $password);
    }

    public function getUtente($db, $codice)
    {
      return UtenteDataManager::getInstance()->get($db, $codice);
    }

    public function getDisciplinaByCodice($connection, $codice)
    {
      return UtenteDataManager::getInstance()->getDisciplinaByCodice($connection, $codice);
    }

    public function updateUtente($connection, $codice, $attivo, $username, $nome, $cognome, $email)
    {
      UtenteDataManager::getInstance()->updateUtente($connection, $codice, $attivo, $username, $nome, $cognome, $email);
    }

    public function updatePassword($connection, $codice, $oldPassword, $newPassword)
    {
      $actualPassword = UtenteDataManager::getInstance()->getUserPassword($connection, $codice);
      //     echo "actual pw : " .$actualPassword;
      if ($actualPassword == md5($oldPassword))
      {
        UtenteDataManager::getInstance()->updateUtentePassword($connection, $codice, $newPassword);
        return 1;
      }
      else
        return 0;
    }

    public function getStudentiByUtente($connection, $codice)
    {
      return UtenteDataManager::getInstance()->getStudentiByUtente($connection, $codice);
    }

    public function sendMailStudente($nome, $cognome, $email, $username, $password)
    {
       $header = "From: IperTestOnLine<info@softwareeasy.it>\n";
       $header .= "MIME-Version: 1.0\n";
       $header .= "Content-Type: text/html;  charset=UTF-8\n";
       $header .= "Content-Transfer-Encoding: 7bit\n\n";
       $oggetto = "nuova registrazione Studente Iper Test On Line";
       $messaggio = '<html><body>Gentile studente ' . $nome . ' ' . $cognome . '<br /><br />è stato creato un nuovo account sulla piattaforma Iper Test on Line. <br /><br />Potrai accedervi attraverso le seguenti credenziali: <br />Username: ' . $username . '<br />Password : ' . $password . '<br /><br />Per effettuare il login incolla il seguente indirizzo sul tuo browser: http://www.softwareeasy.it/school/portal/newLogin.php<br /><br />Distinti saluti<br/>Iper Test On Line Admin</body></html>';

      mail($email, $oggetto, $messaggio, $header);
    }

  }

?>
